package com.ichart.js;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.ichart.doc.Info;
import com.ichart.utils.FileUtils;

/**
 * merge JS create at 2011-12-2 下午02:19:53
 * 
 * @author wanghe
 */
public class ScriptHelp {
	private static int fileNum = 0;
	private static String Enter = "\r\n";

	public static void main(String[] args) {
		String toFile = "E:/workspace8.6/sae/js/ichart.all.js";
		merge(toFile, false);
	}
	
	public static List<String> getChartJs() {
		List<String> list = new ArrayList<String>();
		list.add("ichart.element.js");
		list.add("ichart.painter.js");
		list.add("ichart.html.js");
		list.add("ichart.component.js");
		list.add("ichart.tip.js");
		list.add("ichart.crosshair.js");
		list.add("ichart.legend.js");
		list.add("ichart.label.js");
		list.add("ichart.chart.js");
		list.add("ichart.custom.js");
		list.add("ichart.coordinate.js");
		list.add("ichart.pie.js");
		list.add("ichart.pie2d.js");
		list.add("ichart.donut2d.js");
		list.add("ichart.pie3d.js");
		list.add("ichart.column.js");
		list.add("ichart.column2d.js");
		list.add("ichart.column3d.js");
		list.add("ichart.columnmulti2d.js");
		list.add("ichart.columnmulti3d.js");
		list.add("ichart.bar.js");
		list.add("ichart.bar2d.js");
		list.add("ichart.barmulti2d.js");
		list.add("ichart.line.js");
		list.add("ichart.linebasic2d.js");
		list.add("ichart.area2d.js");
		list.add("ichart.linesegment.js");
		list.add("ichart.rectangle.js");
		list.add("ichart.rectangle2d.js");
		list.add("ichart.rectangle3d.js");
		list.add("ichart.sector.js");
		list.add("ichart.sector2d.js");
		list.add("ichart.sector3d.js");
		list.add("ichart.text.js");
		return list;
	}
	
	public static List<String> getJs() {
		List<String> list = new ArrayList<String>();
		list.add("ichart.core.js");
		list.add("ichart.element.js");
		list.add("ichart.painter.js");
		list.add("ichart.html.js");
		list.add("ichart.component.js");
		list.add("ichart.tip.js");
		list.add("ichart.crosshair.js");
		list.add("ichart.legend.js");
		list.add("ichart.label.js");
		list.add("ichart.text.js");
		list.add("ichart.chart.js");
		list.add("ichart.custom.js");
		list.add("ichart.coordinate.js");
		list.add("ichart.rectangle.js");
		list.add("ichart.rectangle2d.js");
		list.add("ichart.rectangle3d.js");
		list.add("ichart.sector.js");
		list.add("ichart.sector2d.js");
		list.add("ichart.sector3d.js");
		list.add("ichart.pie.js");
		list.add("ichart.pie2d.js");
		list.add("ichart.pie3d.js");
		list.add("ichart.donut2d.js");
		list.add("ichart.column.js");
		list.add("ichart.column2d.js");
		list.add("ichart.column3d.js");
		list.add("ichart.columnmulti2d.js");
		list.add("ichart.columnmulti3d.js");
		list.add("ichart.bar.js");
		list.add("ichart.bar2d.js");
		list.add("ichart.barmulti2d.js");
		list.add("ichart.linesegment.js");
		list.add("ichart.line.js");
		list.add("ichart.linebasic2d.js");
		//list.add("ichart.linemonitor2d.js");
		list.add("ichart.area2d.js");
		//list.add("ichart.points.js");
		//list.add("ichart.scatter2d.js");
		return list;
	}
	
	public static void merge(String toFile, boolean isCompress) {
		mergeJS(getJs(), toFile, isCompress);
	}

	private static void readContent(File file, StringBuffer content) {
		// System.out.println("reading "+file.getName());
		try {
			content.append(Enter);
			content.append(FileUtils.readFileToString(file).replaceAll("iChart","\\$").replaceAll("//@end",""));
		} catch (IOException e) {
			e.printStackTrace();
		}
		fileNum++;
	}

	public static void mergeJS(List<String> file, String toFile,
			boolean isCompress) {
		StringBuffer content = new StringBuffer();
		try {
			for (String string : file) {
				if(string.equalsIgnoreCase("ichart.core.js")){
					content.append(FileUtils.readFileToString(new File(Info.SrcPath + "ichart.core.js")));
					break;	
				}
			}
			
			content.append(Enter+";(function($){");
			
			for (String string : file) {
				if(!string.equalsIgnoreCase("ichart.core.js"))
				readContent(new File(Info.SrcPath + string), content);
			}
			
			content.append(Enter+"})(iChart);");
			
			FileUtils.writeStringToFile(FileUtils.createFile(toFile),
					isCompress ? compressJS(content.toString()) : content
							.toString());
			System.out.println("merge "
					+ fileNum
					+ ",out path:"
					+ toFile
					+ ",size:"
					+ FileUtils.byteCountToDisplaySize(FileUtils
							.sizeOf(FileUtils.createFile(toFile))));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 去除注释、多个空格和换行，最终形成一行的字符串
	 * 
	 * @param content
	 * @return String create at 2011-12-2 下午04:41:24
	 * @author wanghe
	 */
	public static String compressJS(String content) {
		int length = content.length();
		int now = length;
		// *去掉//的注释
		content = Pattern.compile("//.*(\\r|\\n|\\r\\n)").matcher(content)
				.replaceAll("");
		System.out.println("去掉//的注释:" + (now - content.length()));
		now = content.length();
		/* 替换换行和回车 */
		content = Pattern.compile("\\n").matcher(content).replaceAll("");
		System.out.println("替换换行和回车:" + (now - content.length()));
		now = content.length();
		// content = Pattern.compile("\\r\\n").matcher(content).replaceAll("");

		// 去掉/*some code*/的注释
		content = Pattern.compile("/\\*.*?\\*/", Pattern.DOTALL).matcher(content).replaceAll("");
		System.out.println("去掉/*some code*/的注释:" + (now - content.length()));
		now = content.length();
		// 多余的空格
		content = Pattern.compile("\\s{2,}").matcher(content).replaceAll(" ");
		System.out.println("多余的空格:" + (now - content.length()));
		now = content.length();
		// 两边的空格去掉
		content = Pattern.compile("\\s+([=}{:,;!\\?&\\+\\-\\×/\\|\\(\\)])\\s*")
				.matcher(content).replaceAll("$1");
		content = Pattern.compile("\\s*([=}{:,;!\\?&\\-\\|\\(\\)])\\s+")
				.matcher(content).replaceAll("$1");
		System.out.println("两边的空格去掉:" + (now - content.length()));
		now = content.length();
		// 加入分号;
		content = Pattern.compile("}([^;,\\\\}\\)\\(\\|\\]])").matcher(content)
				.replaceAll("};$1");
		content = Pattern.compile("};(else|catch)").matcher(content)
				.replaceAll("}$1");
		// content =
		// Pattern.compile("\\)([^;}{,&\\|\\)\\(\\+\\-\\*/])").matcher(content).replaceAll(");$1");
		// content =
		// Pattern.compile("\\)(var|return)").matcher(content).replaceAll(");$1");
		System.out.println("加入分号:" + (content.length() - now));

		System.out.println("文件大小:" + length / 1000 + "KB,共" + length
				+ "个字符,压缩到:" + content.length() + "个字符,压缩率为:"
				+ (length - content.length()) * 100 / length + "%");
		return content;
	}
	/*
	 * private static void writeFile(String content,String comspec) { try { int
	 * i = comspec.lastIndexOf("/"); String dirs = comspec.substring(0,i); File
	 * file = new File(dirs); if(!file.exists()){ file.mkdir(); } file = new
	 * File(comspec); if(!file.exists()){ file.createNewFile(); }
	 * FileOutputStream fos = new FileOutputStream(file); Writer out = new
	 * OutputStreamWriter(fos,ENCODE); out.write(content); out.close();
	 * fos.close(); } catch (IOException e) { e.printStackTrace(); } }* /*
	 * private static String readFile(File file) { StringBuilder sb = new
	 * StringBuilder(); try { InputStreamReader read = new InputStreamReader
	 * (new FileInputStream(file),ENCODE); BufferedReader reader=new
	 * BufferedReader(read); String s = reader.readLine(); while (s != null) {
	 * sb.append(s); sb.append("\r\n"); s = reader.readLine(); } reader.close();
	 * } catch (IOException e) { e.printStackTrace(); } return sb.toString(); }
	 */

}